package com.enation.app.shop.front.policyInsurance.webController;


import com.enation.app.shop.core.goods.model.Goods;
import com.enation.app.shop.core.goods.service.IGoodsManager;
import com.enation.app.shop.front.api.inpolicy.entity.InPolicy;
import com.enation.app.shop.front.policyInsurance.entity.InsuranceCheck;
import com.enation.app.shop.front.policyInsurance.layuiGrid.LayuiResponse;
import com.enation.app.shop.front.policyInsurance.layuiGrid.LayuiUtil;
import com.enation.app.shop.front.policyInsurance.service.InsuranceCheckMapper;
import com.enation.app.shop.front.policyInsurance.service.InsuranceProtCheckManage;
import com.enation.app.shop.front.policyInsurance.service.InsuranceProtOrdMapper;
import com.enation.framework.action.GridJsonResult;
import com.enation.framework.action.JsonResult;
import com.enation.framework.util.JsonResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Timestamp;
import java.util.*;

@Scope("prototype")
@Controller
@RequestMapping("/web/InPolicyOrd")
public class InPolicyOrdController extends GridJsonResult {

    @Autowired
    private InsuranceProtCheckManage insuranceProtCheckManage;
    @Autowired
    private InsuranceCheckMapper insuranceCheckMapper;
    @Autowired
    private IGoodsManager iGoodsManager;
    @Autowired
    private InsuranceProtOrdMapper insuranceProtOrdMapper;


    @RequestMapping("/getAllInsuranceProtOrd")
    public ModelAndView getAllInsuranceProtOrd(){
        ModelAndView modelAndView = new ModelAndView();

        return modelAndView;
    }













    //添加供应商跳转页面
    @RequestMapping(value="/getAllIProtOrd.do")
    public ModelAndView getAllIProtOrd(Model model){
        ModelAndView view = new ModelAndView();
        view.setViewName("/shop/admin/appinsurance/insuranceOrderList");
        return view;
    }
    //跳转投保
    @RequestMapping(value="/addInsurancOrder.do")
    public ModelAndView addInsurancOrder(Model model){
        ModelAndView view = new ModelAndView();
        List<Goods> goods=iGoodsManager.getAllInPolicyName();
        List list=new ArrayList();
        list.add("其他");
        view.addObject("goods",goods);
        view.setViewName("/shop/admin/appinsurance/addInsuranceOrder");
        return view;
    }


    /**
     * 获取全部信用额度支付的保险订单
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAllIProtOrd1")
    public LayuiUtil getAllInsuranceProtOrd(Integer page, Integer limit, String inPolicy_number){
            List<InPolicy> inPolicyList=insuranceProtOrdMapper.getAllCreaditOrder(page-1,limit,inPolicy_number);
            if(inPolicyList!=null){
                return LayuiResponse.getObjectMessageJson(inPolicyList,"",inPolicyList.size());
            }else {
                return LayuiResponse.getObjectMessageJson(null,"获取数据失败!",null);
            }
    }

    /**
     * 信用额度支付审核
     * @param inPolicy_id
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkInPolicyOrd")
    public JsonResult checkInPolicyOrd(Integer inPolicy_id, Integer checkType){
        try {
            Map map=new HashMap();
            InsuranceCheck insuranceCheck=insuranceCheckMapper.selectByWarrantyId(inPolicy_id);
            Calendar cal=Calendar.getInstance();
            Timestamp timestamp=new Timestamp(System.currentTimeMillis());
            cal.setTime(insuranceCheck.getBegin_time());
            if(checkType==1){
                if(timestamp.after(cal.getTime())){
                    map.put("begin_time",insuranceCheck.getBegin_time());
                    cal.setTime(insuranceCheck.getBegin_time());
                    cal.add(Calendar.MONTH,12);
                    map.put("end_time",new Timestamp(cal.getTime().getTime()-1000*60*60*24));
                }else {
                    map.put("begin_time",timestamp);
                    cal.setTime(timestamp);
                    cal.add(Calendar.MONTH,12);
                    map.put("end_time",new Timestamp(cal.getTime().getTime()-1000*60*60*24));
                }
                map.put("is_payment",1);
                map.put("status",6);
                map.put("update_time",new Timestamp(System.currentTimeMillis()));
                insuranceProtCheckManage.updateInPolicyStatus(inPolicy_id,map);
            }else {
                map.put("status",5);
                map.put("is_payment",0);
                map.put("update_time",new Timestamp(System.currentTimeMillis()));
                insuranceProtCheckManage.updateInPolicyStatus(inPolicy_id,map);
            }
            return JsonResultUtil.getSuccessJson("操作成功!");
        }catch (Exception e){
            e.printStackTrace();
            return JsonResultUtil.getErrorJson("操作失败!");
        }
    }
}
